// 插入

// swagger 文档
defineRouteMeta({
  openAPI: {
    description: '添加用户信息',
    summary: '添加用户信息',
    tags: ['用户'],

    requestBody: {
      content: {
        'application/x-www-form-urlencoded': {
          schema: {
            type: 'object',
            properties: {
              name: {
                description: '用户名',

                type: 'string'
              },
              password: {
                description: '用户密码',

                type: 'string'
              },
              email: {
                description: '用户邮箱',

                type: 'string'
              },
              avatar: {
                description: '用户头像',

                type: 'string'
              }
            },
            required: ['name', 'password', 'email', 'avatar']
          }
        }
      },
      required: true
    },

    responses: {
      200: {
        description: '用户添加成功',
        content: {
          'application/json': {
            schema: {
              type: 'object',
              properties: {
                code: {
                  type: 'number',
                  description: '状态码',
                  default: 200
                },
                message: {
                  type: 'string',
                  description: '状态信息'
                },
                data: {
                  type: 'null',
                  description: '用户信息',
                  default: null
                }
              }
            }
          }
        }
      },
      400: {
        description: '参数错误',
        content: {
          'application/json': {
            schema: {
              type: 'object',
              properties: {
                code: {
                  type: 'number',
                  description: '状态码',
                  default: 400
                },
                message: {
                  type: 'string',
                  description: '状态信息'
                },
                data: {
                  type: 'null',
                  description: '用户信息',
                  default: null
                }
              }
            }
          }
        }
      },

      500: {
        description: '用户添加失败',
        content: {
          'application/json': {
            schema: {
              type: 'object',
              properties: {
                code: {
                  type: 'number',
                  description: '状态码',
                  default: 500
                },
                message: {
                  type: 'string',
                  description: '错误信息'
                },
                data: {
                  type: 'null',
                  description: '用户信息',
                  default: null
                }
              }
            }
          }
        }
      }
    }
  }
});

export default eventHandler(async event => {
  const { name, email, password, avatar } = await readBody(event);
  const { result } = new useResult(event);

  // 验证
  if (!name || !email || !password || !avatar) {
    return result(null, '参数错误', 400);
  }

  const reg = /^[A-Za-z0-9\u4e00-\u9fa5]+@[a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+)+$/;
  if (!reg.test(email)) {
    return result(null, '邮箱格式错误', 400);
  }

  try {
    const users = await useDrizzle()
      .insert(tables.users)
      .values({
        name,
        email,
        password,
        avatar,
        createdAt: new Date()
      })
      .returning();
    // return users;
    return result(null, '添加成功', 200);
  } catch (e: any) {
    return result(null, e.message, 500);
  }
});
